package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @RequestMapping("loginInfo")
    @ResponseBody
    public Result login(String oname,String pwd) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(oname,pwd);
        try {
            subject.login(token);
            return Result.respones(ResultEnum.SUCCESS).setData("登录成功");
        }catch (UnknownAccountException u) {
            return Result.respones(ResultEnum.ERROR).setData("用户名不存在");
        }catch (IncorrectCredentialsException i) {
            return Result.respones(ResultEnum.ERROR).setData("密码错误");
        }
    }
}
